﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using LuxDiner.Model.Orders;
using LuxDiner.Command.Orders;
using Utils;
namespace LuxDiner.Logic.Orders
{
    public class ProductOrderLogic
    {
        public static ProductOrderModel NewOrder(ProductOrderModel order)
        {
            if (order != null)
            {
                if (order.Items == null || order.Items.Count == 0)
                    throw new LuxDinerException("没有内容");
                if (order.HouseID == 0)
                    throw new LuxDinerException("请选择一个仓库");
                var house = WarehouseLogic.GetModel(order.HouseID);
                if (house == null)
                    throw new ApplicationException("仓库无效");
                
                order.CreatedOn = DateTime.Now;
                order.TotalMoney = order.Items.Sum(p => p.CostMoney);
                foreach (var it in order.Items)
                {
                    it.Retention = it.Retention.Trim();
                    if (it.Quantity <= 0)
                        throw new ApplicationException("无效的数量");
                    var product = Products.ProductLogic.GetModel(it.ProductID);
                    if (product == null)
                        throw new ApplicationException("无效的产品");
                    it.Product = product;
                    if (string.IsNullOrEmpty(it.Retention))
                        throw new ApplicationException("请提供批号");
                }
                return ProductOrderCommand.NewOrder(order);
            }
            return null;
        }
        
        public static ProductOrderCollection GetOrders(ProductOrderQuery sea)
        {
            return ProductOrderCommand.GetOrders(sea);
        }

        public static ProductOrderModel GetOrder(string orderNo)
        {
            return ProductOrderCommand.GetOrder(orderNo);
        }

        public static ProductOrderModel SetBalance(ProductOrderModel order, int targetHouseID = 0)
        {
            if (order != null)
            {
                if (order.Items == null || order.Items.Count == 0)
                    throw new LuxDinerException("没有内容");
                if (order.HouseID == 0)
                    throw new LuxDinerException("请选择一个仓库");
                var house = WarehouseLogic.GetModel(order.HouseID);
                if (house == null)
                    throw new ApplicationException("仓库无效");
                if (order.Method == (int)OrderMethod.TB)
                {
                    if (targetHouseID == 0)
                        throw new LuxDinerException("请选择目标仓库");
                    house = WarehouseLogic.GetModel(targetHouseID);
                    if (house == null)
                        throw new ApplicationException("目标仓库无效");
                }

                order.CreatedOn = DateTime.Now;
                order.TotalMoney = order.Items.Sum(p => p.CostMoney);
                foreach (var it in order.Items)
                {
                    it.Retention = it.Retention.Trim();
                    if (it.Quantity == 0)
                        throw new ApplicationException("无效的数量");
                    if (order.Method == (int)OrderMethod.TB && it.Quantity < 0)
                        throw new ApplicationException("调拨的数量不能小于零。");
                    var product = Products.ProductLogic.GetModel(it.ProductID);
                    if (product == null)
                        throw new ApplicationException("无效的产品");
                    it.Product = product;
                    if (string.IsNullOrEmpty(it.Retention))
                        throw new ApplicationException("请提供批号");
                }
                return ProductOrderCommand.SetBalance(order, targetHouseID);
            }
            return null;
        }

        public static VProductOrderDetailsCollection GetOrderDetails(ProductOrderQuery sea)
        {
            return ProductOrderCommand.GetOrderDetails(sea);
        }
    }
}
